/**
 * FileName: SaloController
 *
 * @author: 阿张
 * Date:     2021/3/4 10:23
 */
package com.qx.controller;

import com.qx.factory.OperationFactory;
import com.qx.pojo.Num;
import com.qx.pojo.Salo;
import com.qx.pojo.SaloNotes;
import com.qx.pojo.ScUser;
import com.qx.service.SaloService;
import com.qx.service.OperationContext;
import com.qx.service.SaloNotesService;
import org.springframework.web.bind.annotation.*;
import result.Result;
import result.ResultFactory;
import util.Down;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/app/salo")
public class SaloController {

    @Resource
    private SaloService saloService;

    @Resource
    private SaloNotesService saloNotesService;

    @RequestMapping(value = "/getList",method = RequestMethod.GET)
    public Result list(){
        return ResultFactory.buildSuccessResult(saloService.getSalo());
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@RequestBody Salo salo){
        return ResultFactory.buildSuccessResult(saloService.save(salo));
    }

    @RequestMapping(value = "/del/{id}",method = RequestMethod.DELETE)
    public Result del(@PathVariable("id") Integer id){
        return ResultFactory.buildSuccessResult(saloService.removeById(id));
    }

    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public Result update(@RequestBody Salo salo){
        return ResultFactory.buildSuccessResult(saloService.updateById(salo));
    }

    @RequestMapping("money")
    public boolean getMoney(){
        OperationContext operationContext = new OperationContext();
        OperationFactory operationFactory = new OperationFactory();
        operationContext.setDistribution(operationFactory.getDistribution("add"));
        ScUser scUser = new ScUser();
        scUser.setRemake("");
        scUser.setUsername("1");
        List<Salo> salos = saloService.selectByUid(1);
        Salo salo = salos.get(0);
        Num v = operationContext.doOperation((int)Math.round(salo.getLowNum() * 100), 30, 50, scUser, salo);
        System.out.println(salo.getSaloNum());
        if (v.getKnife() != 0){
            System.out.println(v.getKnife());
            System.out.println(v.getNum());
            salo.setLowNum(Math.round(salo.getLowNum())-v.getKnife());
            salo.setSaloNum(v.getNum());
            saloService.updateById(salo);
            SaloNotes saloNotes = new SaloNotes();
            saloNotes.setSaloId(salo.getId());
            saloNotes.setUserId(scUser.getId());
            saloNotes.setSaloPrice(salo.getLowNum()-v.getKnife());
            saloNotesService.save(saloNotes);
            return true;
        }
        System.out.println(v);
        return false;
    }

    @RequestMapping("downLoad")
    public void downLoad(HttpServletResponse response) throws IOException {
        Down down = new Down();
        down.downloadLocal(response);
    }
}
